/**
 * 将小于10的值加上零
 * @param {number} value 数字
 * @return {string}
 */
const formatNumber = (value: string | number) => `${value < 10 ? "0" + value : value}`;

/**
 * 时间戳转日期
 * @param {number} timestamp 时间戳
 * @param {传入格式} format 必须以Y-M-D h:m:s组成，默认格式为以上
 */
const timeStampToTime = (timestamp: string | number | Date, format = "Y-M-D h:m:s") => {
    // @ts-ignore
    timestamp = timestamp.toString().length === 10 ? timestamp * 1000 : timestamp;
    const formatArr = ["Y", "M", "D", "h", "m", "s"];
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = formatNumber(date.getMonth() + 1);
    const day = formatNumber(date.getDate());
    const hour = formatNumber(date.getHours());
    const minute = formatNumber(date.getMinutes());
    const second = formatNumber(date.getSeconds());
    const time = [year, month, day, hour, minute, second];
    time.forEach((item, index) => {
        // @ts-ignore
        format = format.replace(formatArr[index], item);
    });
    return format;
};

export {timeStampToTime}
// module.exports = {timeStampToTime}